# coding=utf-8
import os

import pexpect

project_root = os.path.abspath(os.path.dirname(__file__))

sqlite_db = os.path.join(project_root, '../../example.db')
useless_table_file = os.path.join(project_root, 'useless-table.sql')
dump_to_mysql_py_path = os.path.join(project_root, 'dump_to_mysql.py')
dump_file = os.path.join(project_root, 'dump.sql')
after_migrate_file=os.path.join(project_root, 'after_migrate.sql')
ucloud_agent_scheme_file = os.path.join(project_root, 'ucloud-agent-schema.sql')

mysql_db_name = 'ucloud_agent'
mysql_username = 'root'
mysql_password = 'winit2015'

useless_tables = [
    '_app_old_20161115',
    'alembic_version',
    'apscheduler_jobs',
    'oracle',
    'user',
]


def clean_useless_sqlite_table():
    print '## 0. Clean useless sqlite table. ##'
    sh = '/bin/bash -c "sqlite3 %s < %s"' % (sqlite_db, useless_table_file)
    print '  start execute shell : %s' % sh
    output, status = pexpect.run(sh, withexitstatus=1, timeout=60)
    print '  shell status: %s, output: %s' % (status, output)
    if status == 0:
        print '  Clean useless sqlite table success'
    else:
        print '  Clean useless sqlite table failure, cause: %s.' % output


def export_from_sqlite():
    if os.path.exists(dump_file):
        os.remove(dump_file)
    print '## 1. Export data from sqlite db : %s ##' % sqlite_db
    sh = '/bin/bash -c "sqlite3 %s .dump | python %s > %s"' % (sqlite_db, dump_to_mysql_py_path, dump_file)
    print '  start execute shell : %s' % sh
    output, status = pexpect.run(sh, withexitstatus=1, timeout=60)
    print '  shell status: %s, output: %s' % (status, output)
    if status == 0:
        print '  Export data success'
    else:
        print '  Export data failure, cause: %s.' % output


def create_database():
    print '## 2. Create mysql database : %s ##' % mysql_db_name
    sh = '/bin/bash -c "mysql -u%s -p%s < %s"' % (
        mysql_username, mysql_password, ucloud_agent_scheme_file)
    print '  start execute shell : %s' % sh
    output, status = pexpect.run(sh, withexitstatus=1, timeout=60)
    print '  shell status: %s, output: %s' % (status, output)
    if status == 0:
        print '  Create mysql database success.'
    else:
        print '  Create mysql database failure, cause: %s.' % output


def import_to_mysql():
    print '## 3. Import data to mysql db : %s ##' % mysql_db_name
    sh = '/bin/bash -c "mysql -u%s -p%s %s < %s"' % (
        mysql_username, mysql_password, mysql_db_name, dump_file)
    print '  start execute shell : %s' % sh
    output, status = pexpect.run(sh, withexitstatus=1, timeout=60)
    print '  shell status: %s, output: %s' % (status, output)
    if status == 0:
        print '  Import data success.'
    else:
        print '  Import data failure, cause: %s.' % output



def after_migrate():
    print '## 4. Re-order zone.##'
    sh = '/bin/bash -c "mysql -u%s -p%s %s < %s"' % (
        mysql_username, mysql_password, mysql_db_name, after_migrate_file)
    print '  start execute shell : %s' % sh
    output, status = pexpect.run(sh, withexitstatus=1, timeout=60)
    print '  shell status: %s, output: %s' % (status, output)
    if status == 0:
        print '  Re-order zone success.'
    else:
        print '  Re-order zone failure, cause: %s.' % output


def run():
    clean_useless_sqlite_table()

    export_from_sqlite()

    create_database()

    import_to_mysql()

    after_migrate()


if __name__ == "__main__":
    run()
